using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Voodoo.Pattern.Random
{
	public static class RandomizePseudo
	{
		public static string GetRandomPseudo(PseudoPool _list, int _minCharacterCount = 1, int _maxCharacterCount = -1)
		{
			if (_list == null || _list._strings.Count == 0)
			{
				return "";
			}
			return _list.GetRandomString(_minCharacterCount, _maxCharacterCount);
		}

		public static string GetRandomPseudo(List<PseudoPool> _lists, int _minCharacterCount = 1, int _maxCharacterCount = -1)
		{
			if (_lists == null || _lists.Count == 0)
			{
				return "";
			}
			return _lists[UnityEngine.Random.Range(0, _lists.Count)].GetRandomString(_minCharacterCount, _maxCharacterCount);
		}

		public static List<string> GetRandomPseudos(PseudoPool _list, int _numberRequired, bool _duplicateAllowed = false, int _minCharacterCount = 1, int _maxCharacterCount = -1)
		{
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			if (_list == null || _list._strings.Count == 0 || _numberRequired <= 0)
			{
				return list;
			}
			if (_list._strings.Count < _numberRequired && !_duplicateAllowed)
			{
				Debug.Log("Not enough element in " + _list.name + " to have no duplicate");
				_duplicateAllowed = true;
			}
			if (!_duplicateAllowed)
			{
				list2 = new List<string>(_list._strings);
			}
			for (int i = 0; i < _numberRequired; i++)
			{
				if (_duplicateAllowed)
				{
					list.Add(GetRandomPseudo(_list));
					continue;
				}
				int index = UnityEngine.Random.Range(0, list2.Count);
				list.Add(list2[index]);
				list2.RemoveAt(index);
			}
			return list;
		}

		public static List<string> GetRandomPseudos(List<PseudoPool> _lists, int _numberRequired, bool _duplicateAllowed = false, int _minCharacterCount = 1, int _maxCharacterCount = -1)
		{
			List<string> list = new List<string>();
			List<string> first = new List<string>();
			if (_lists == null || _numberRequired <= 0)
			{
				return list;
			}
			if (_maxCharacterCount != -1 && _maxCharacterCount < _minCharacterCount)
			{
				Debug.LogWarning("Error string size asked with min:" + _minCharacterCount + " max:" + _maxCharacterCount + " set _maxCharacterCount to -1 (infini)");
				_maxCharacterCount = -1;
				if (_minCharacterCount == -1)
				{
					Debug.LogWarning("Error _minCharacterCount can't be equal to -1 (infiny) set to 1");
					_minCharacterCount = 1;
				}
			}
			if (!_duplicateAllowed)
			{
				int num = 0;
				foreach (PseudoPool _list in _lists)
				{
					num += _list._strings.Count;
				}
				if (num < _numberRequired)
				{
					Debug.Log("Not enough element in _lists to have no duplicate");
					_duplicateAllowed = true;
				}
				else
				{
					foreach (PseudoPool _list2 in _lists)
					{
						first = first.Concat(_list2._strings).ToList();
					}
				}
			}
			for (int i = 0; i < _numberRequired; i++)
			{
				bool flag;
				string text;
				if (_duplicateAllowed)
				{
					do
					{
						flag = false;
						if (list.Count == 0)
						{
							Debug.LogWarning("No string selection found with min:" + _minCharacterCount + " max:" + _maxCharacterCount + " return random");
							return list;
						}
						int index = UnityEngine.Random.Range(0, list.Count);
						text = list[index];
						list.RemoveAt(index);
						if (text.Length <= _minCharacterCount)
						{
							continue;
						}
						if (_maxCharacterCount != -1)
						{
							if (text.Length < _maxCharacterCount)
							{
								flag = true;
							}
						}
						else
						{
							flag = true;
						}
					}
					while (!flag);
					list.Add(text);
					continue;
				}
				do
				{
					flag = false;
					if (list.Count == 0)
					{
						Debug.LogWarning("No string selection found with min:" + _minCharacterCount + " max:" + _maxCharacterCount + " return random");
						return list;
					}
					int index = UnityEngine.Random.Range(0, list.Count);
					text = list[index];
					list.RemoveAt(index);
					if (text.Length <= _minCharacterCount)
					{
						continue;
					}
					if (_maxCharacterCount != -1)
					{
						if (text.Length < _maxCharacterCount)
						{
							flag = true;
						}
					}
					else
					{
						flag = true;
					}
				}
				while (!flag);
				list.Add(text);
			}
			return list;
		}
	}
}
